/**
 * @format
 * @Author: TC
 * @Date: 2025-06-08 10:52:58
 * @Last Modified by: TC
 * @Last Modified time: 2025-06-19 22:33:29
 */
import { nsCmdConst } from "db://assets/FrameWork/Module/InputCmd/CmdConst";
import { EntityBase } from "../EntityBase";

export class EntityKeyboard {
    private _entity: EntityBase = null;

    public get entity(): EntityBase {
        return this._entity;
    }
    constructor(entity: EntityBase) {
        this._entity = entity;
    }

    onKeyDown(keyCode: number, param?: any): void {
        this.entity.cmd.rdKey[keyCode] = true;
        switch (keyCode) {
            case nsCmdConst.EKeyboardCode.UP:
                this.entity.cmd.v++;
                break;
            case nsCmdConst.EKeyboardCode.DOWN:
                this.entity.cmd.v--;
                break;
            case nsCmdConst.EKeyboardCode.LEFT:
                this.entity.cmd.h--;
                break;
            case nsCmdConst.EKeyboardCode.RIGHT:
                this.entity.cmd.h++;
                break;
            case nsCmdConst.EKeyboardCode.FOLLOW:
                // this.entity.ai.following = true;
                break;
            case nsCmdConst.EKeyboardCode.MOVE:
                // console.log("move", param);
                this.entity.cmd.angle = param;
                break;
            case nsCmdConst.EKeyboardCode.JUMP:
                break;
        }
    }

    onKeyUp(keyCode: number): void {
        this.entity.cmd.rdKey[keyCode] = false;
        switch (keyCode) {
            case nsCmdConst.EKeyboardCode.UP:
                this.entity.cmd.v--;
                break;
            case nsCmdConst.EKeyboardCode.DOWN:
                this.entity.cmd.v++;
                break;
            case nsCmdConst.EKeyboardCode.LEFT:
                this.entity.cmd.h++;
                break;
            case nsCmdConst.EKeyboardCode.RIGHT:
                this.entity.cmd.h--;
                break;
            case nsCmdConst.EKeyboardCode.FOLLOW:
                // this.entity.ai.following = false;
                break;
            case nsCmdConst.EKeyboardCode.MOVE:
                this.entity.cmd.angle = -1;
                this.entity.speed.setAngle(-1);
                break;
            case nsCmdConst.EKeyboardCode.JUMP:
                break;
        }
        this.entity.cmd.cmdItem.removeCode(keyCode);
    }

    clearKey(): void {
        this.entity.cmd.rdKey = [] as any;
        this.entity.cmd.h = this.entity.cmd.v = 0;
        this.entity.cmd.cmdItem.clear();
    }
}
